<?php

namespace app\admin\controller;
use app\common\entity\BackResult;
use app\common\service\CategoryService;
use app\Request;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/10 0010
 * Time: 21:16
 */
class Category extends Base
{
    /**
     * 后台分类
     * @param CategoryService $categoryService
     * @return \think\Response
     */
    public function tree(CategoryService $categoryService)
    {
        return BackResult::success($categoryService->tree());
    }

    public function create(Request $request)
    {
        $data = $request->post();
        //参数检查那些的以后上线了再做，先跑通
        $service = new CategoryService();
        $data = $service->create($data);
        return BackResult::success($data);
    }

    public function update(Request $request)
    {
        $data = $request->post();
        $id = $request->param("id");
        if(empty($id)){
            return BackResult::error([],"缺少id");
        }
        $service = new CategoryService();
        $data = $service->update($data,$id);
        return BackResult::success($data);
    }

    public function remove(Request $request)
    {
        $id = $request->param("id");
        if(empty($id)){
            return BackResult::error([],"缺少id");
        }
        $service = new CategoryService();
        $service->remove($id);
        return BackResult::success([]);
    }

    public function one(Request $request)
    {
        $id = $request->param("id");
        if(empty($id)){
            return BackResult::error([],"缺少id");
        }
        $service = new CategoryService();
        $data = $service->getOne($id);
        return BackResult::success($data);
    }
}